package com.ccl.iot.asynctask;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.publics.Messager;
import com.ccl.iot.views.SubBlirControlView;

import java.util.ArrayList;

import cn.com.broadlink.sdk.BLLet;
import cn.com.broadlink.sdk.constants.controller.BLControllerErrCode;
import cn.com.broadlink.sdk.constants.controller.BLDeviceState;
import cn.com.broadlink.sdk.data.controller.BLStdData;
import cn.com.broadlink.sdk.param.controller.BLStdControlParam;
import cn.com.broadlink.sdk.result.controller.BLStdControlResult;

public class SendIrTask extends CustomTask<String, Void, BLStdControlResult> {
    private IOTSubBLir mSubBLir;
    private View mOfflineMask;
    private SubBlirControlView.OnDevOnlineChangeListener mOnlineChangeListener;

    public SendIrTask(Context context, IOTSubBLir subBlir, SubBlirControlView.OnDevOnlineChangeListener l, View mask) {
        super(context, "发送中...");
        mSubBLir = subBlir;
        mOnlineChangeListener = l;
        mOfflineMask = mask;
    }

    @Override
    protected BLStdControlResult doInBackground(String... params) {
        CodeviewBean codeviewBean = mSubBLir.getIRCode().get(params[0]);
        //设置要发送的红外指令
        BLStdData.Value value = new BLStdData.Value();
        if (codeviewBean != null) {
            value.setVal(codeviewBean.aCode);
        }else{
            value.setVal(params[0]);
        }

        ArrayList<BLStdData.Value> irVals = new ArrayList<BLStdData.Value>();
        irVals.add(value);

        /**发送学习到的命令**/
        BLStdControlParam intoStudyParam = new BLStdControlParam();
        intoStudyParam.setAct(BlirConst.ACT_SET);
        intoStudyParam.getParams().add("irda");
        intoStudyParam.getVals().add(irVals);
        BLStdControlResult blStdControlResult = BLLet.Controller.dnaControl(mSubBLir.getDid(), null, intoStudyParam);
        if (blStdControlResult != null && blStdControlResult.getStatus() != BLControllerErrCode.SUCCESS) {
            int state = BLLet.Controller.queryDeviceState(mSubBLir.getDid());
            if (state == 3) {
                blStdControlResult.setStatus(state);
            }
            Log.e("SendIr", "queryDeviceState state: " + state);
        }
        return blStdControlResult;
    }

    @Override
    protected void onPostExecute(BLStdControlResult stdControlResult) {
        super.onPostExecute(stdControlResult);
        Log.e("SendIr", "State: " + stdControlResult.getStatus() + ", Msg: " + stdControlResult.getMsg());
        if (stdControlResult != null && stdControlResult.getStatus() == BLControllerErrCode.SUCCESS) {
            Messager.Show("发送成功");
        } else if (stdControlResult != null && stdControlResult.getStatus() == BLDeviceState.OFFLINE) {
            Messager.Show("设备已经离线");
            mSubBLir.setRealOnline(false);
            if (mOnlineChangeListener != null) {
                mOnlineChangeListener.onDevOnlineChange();
            } else {
                mOfflineMask.setVisibility(View.VISIBLE);
            }
        } else {
            Messager.Show("发送失败");
        }
    }

    public static void sendCodeByCode(String did, String params) {
        if (params != null) {

            BLStdData.Value value = new BLStdData.Value();
            value.setVal(params);

            ArrayList<BLStdData.Value> irVals = new ArrayList<BLStdData.Value>();
            irVals.add(value);

            /**发送学习到的命令**/
            BLStdControlParam intoStudyParam = new BLStdControlParam();
            intoStudyParam.setAct(BlirConst.ACT_SET);
            intoStudyParam.getParams().add("irda");
            intoStudyParam.getVals().add(irVals);
            BLStdControlResult blStdControlResult = BLLet.Controller.dnaControl(did, null, intoStudyParam);
            Log.e("ExecuteBLIR", "did: "+did+", blStdControlResult: " + blStdControlResult.getStatus() + ", " + blStdControlResult.getMsg());
            Log.e("ExecuteBLIR", "Code: "+params);
        }
    }

}
